<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" type="text/css" href="style.css" media="screen" title="Lolut ETU" />
  <title>Hébergement Lolut ETU</title>
</head>
<body>
  <h1>Hébergement Lolut ETU</h1>

  <p id="menu">
    <a href="index.php">Accueil</a>
  </p>

  <div id="main">

<?php (!empty($_SERVER['PHP_AUTH_USER'])) or die('Script misconfigured, no auth!');

// The path to the htpasswd file.
$passwdfile = '/srv/projetu/authn.conf';

$user = $_SERVER['PHP_AUTH_USER'];

if (isset($_POST['changepass']))
{
  $p1 = $_POST['pass'];
  $p2 = $_POST['confirm'];

  $output = array();
  $ret = 0;

  // If the passwords are valid
  if (!empty($p1) && ($p1 == $p2)) {
    exec('/usr/bin/htpasswd2 -s -b ' . escapeshellarg($passwdfile) .
         ' ' . escapeshellarg($user) . ' ' . escapeshellarg($p1), &$output, &$ret);

    if (0 !== $ret) {
      die('Error changing password!');
    }
    /*
    // Open the file and get its contents
    $passwdcts  = file_get_contents($passwdfile) or die('Impossible de lire le fichier authn!');
    $entries = split("\n", $passwdcts);

    // Reopen the password file in write mode, and start outputting entries
    $fh = fopen($passwdfile, "w+") or die('Unable to open the passwords file for writing!');
    foreach ($entries as $entry) {
      $data = split(":", $entry);
      if (is_array($data)) {

	// If the current entry is our user, change its password
	if ($data[0] == $user) {
	  fwrite($fh, "$user:{SHA}" .
		 base64_encode(sha1($p1, true)) . "=\n");
	} else {
	  // Otherwise just output the entry as-is
	  fwrite($fh, $entry . "\n");
	}
      }*/
  //fclose($fh);

    echo "<span style=\"color: green;\">Mot de passe changé.</span>\n";
  } else {
    echo "<span style=\"color: red;\">Aucun mot de passe fourni, ou les mots de passe ne correspondent pas.</span>\n";
  }
}

?>

  <h2>Changement de mot de passe</h2>
  <p>Utilisez le formulaire suivant pour changer de mot de passe:</p>

  <form action="chpass.php" method="post">
    <label for="pass">Nouveau mot de passe : </label><input type="password" name="pass" /><br />
    <label for="confirm">Confirmation : </label><input type="password" name="confirm" /><br />
    <input type="submit" name="changepass" value="Changer mon mot de passe" />
  </form>
  </div>
</body>
</html>
